/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.core.validate.QueryGroup;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdResultBo;
import com.comac.ins.isfd.domain.vo.IsfdResultVo;
import com.comac.ins.isfd.domain.vo.ResultCountObjectVo;
import com.comac.ins.isfd.service.IIsfdResultService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 结果库controller
 *
 * @author zhouyaoxin
 * @date 2024-7-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/result")
public class IsfdResultController extends BaseController {

    @Autowired
    private IIsfdResultService IIsfdResultService;

    /**
     * 按结果数据文件类型,统计结果数据数量
     */
    @SaCheckPermission("isfd:result:count")
    @GetMapping("/list/count")
    public R<List<ResultCountObjectVo>> listCount() {
        return R.ok(IIsfdResultService.listCount());
    }

    /**
     * 结果库分页列表查询
     */
    @SaCheckPermission("isfd:result:list")
    @GetMapping("/list")
    public TableDataInfo<IsfdResultVo> list(@Validated(QueryGroup.class) IsfdResultBo bo, PageQuery pageQuery) {
        return IIsfdResultService.queryPageList(bo, pageQuery);
    }

    /**
     * 结果库新增保存
     *
     * @param file 文件
     */
    @SaCheckPermission("isfd:result:save")
    @Log(title = "上传result + 详细信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<IsfdResultVo> save(@RequestParam("file") MultipartFile file,
                               @RequestParam("resultName") String resultName,
                               @RequestParam("resultType") String resultType,
                               @RequestParam("applicableResult") String applicableResult,
                               @RequestParam("resultSeries") String resultSeries,
                                @RequestParam("loadConditions") String loadConditions,
                                @RequestParam("resultScope") String resultScope) {
        IsfdResultBo isfdResultBo = new IsfdResultBo();
        isfdResultBo.setResultName(resultName);
        isfdResultBo.setResultType(resultType);
        isfdResultBo.setApplicableResult(applicableResult);
        isfdResultBo.setResultSeries(resultSeries);
        isfdResultBo.setLoadConditions(loadConditions);
        isfdResultBo.setResultScope(resultScope);
        IsfdResultVo isfdResultVo = IIsfdResultService.save(file, isfdResultBo);
        return R.ok(isfdResultVo);
    }

    /**
     * 结果库下载文件
     *
     * @param resultId 结果库id
     */
    @SaCheckPermission("isfd:result:download")
    @GetMapping("/download/{resultId}")
    public void download(@PathVariable Long resultId, HttpServletResponse response) throws IOException {
        IIsfdResultService.download(resultId, response);
    }

    /**
     * 删除结果库对象
     *
     * @param resultIds 结果库id数组
     */
    @SaCheckPermission("isfd:result:remove")
    @Log(title = "结果库删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{resultIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] resultIds) {
        return toAjax(IIsfdResultService.deleteWithValidByIds(List.of(resultIds), true));
    }

    /**
     * 更新结果库对象
     *
     * @param isfdResultBo 结果库对象
     */
    @SaCheckPermission("isfd:result:remove")
    @Log(title = "结果库更新")
    @PostMapping("/update")
    public R<Void> remove(@RequestBody IsfdResultBo isfdResultBo){
        if(isfdResultBo.getResultId() == null ){
            return R.fail("id不能为空");
        }
        IIsfdResultService.update(isfdResultBo);
        return R.ok();
    }
}
